#Working directory to file directory

setwd(dirname(rstudioapi::getActiveDocumentContext()$path))

#Install Required Packages

packages <- c("tidyverse", "stringr")

# Check if each package is installed, install if missing, and load it
for (pkg in packages) {
  if (!require(pkg, character.only = TRUE)) {
    install.packages(pkg)
    require(pkg, character.only = TRUE)
  }
}

rawdata <- read.csv("results_unlabelled.csv")

rawdata = rawdata |> dplyr::filter(!pid7 == 4)

#selecting relevant variables
data = rawdata |> select(starts_with("violation") | starts_with("compliance") |
        ends_with("_choice") | ends_with("party_ord")  | "caseid" | 
          "political_race" | pid7 | democrat_therm_1 | republican_therm_1 | 
          vote_importance | presvote20post | norm_judges | norm_censorship |
          norm_loyalty | newsint | ideo5 | urbanicity2 | gender | race | educ)

data = data |> mutate(humrace = race) |> select(!race)

data = data |> select(!ends_with("pipe"))

#long data format
data = pivot_longer(data, cols = ends_with("choice"), names_to = "race",
                     values_to = "choice")

#number of violations variable
data = data |> mutate(ViolNo = case_when(str_detect(race, "1") ~ 1,
                                           str_detect(race, "2") ~ 2,
                                           TRUE ~ 0))
data = data |> mutate(ViolNo = as.factor(ViolNo))

#choice variables are 0 to 14, recoding odds to zero (cand A) and evens to one (cand B)
data = data |> mutate(choice = case_when(choice %% 2 == 0 ~ 1,
                                           choice %% 2 == 1 ~ 0))

#identifying partisanship of candidate A and B for each level
data = data |> mutate(OrdCand = case_when(startsWith(race, "Baseline") ~ baseline_party_ord,
                                            startsWith(race, "Local1") ~ local1_party_ord,
                                            startsWith(race, "Local2") ~ local2_party_ord,
                                            startsWith(race, "State1") ~ state1_party_ord,
                                            startsWith(race, "State2") ~ state2_party_ord,
                                            startsWith(race, "National1") ~ national1_party_ord,
                                            startsWith(race, "National2") ~ national2_party_ord))
data = data |> select(!ends_with("party_ord"))

#creating the dependent variable (party defections), 0 is choosing in-party, 1 choosing out-party
data = data |> mutate(OutChoice = case_when(OrdCand == 0 & choice == 0 ~ 0,
                                              OrdCand == 0 & choice == 1 ~ 1,
                                              OrdCand == 1 & choice == 0 ~ 1,
                                              OrdCand == 1 & choice == 1 ~ 0))

#Randomized norms
#First
data = data |> mutate(Level1 = case_when(startsWith(race, "Baseline") ~ 0,
                                        startsWith(race, "Local1") ~ violation1_local1,
                                        startsWith(race, "Local2") ~ violation1_local2,
                                        startsWith(race, "State1") ~ violation1_state1,
                                        startsWith(race, "State2") ~ violation1_state2,
                                        startsWith(race, "National1") ~ violation1_national1,
                                        startsWith(race, "National2") ~ violation1_national2))
data = data |> mutate(Level1 = case_when(Level1 == 0 ~ "Compliance", 
                                         Level1 == 1 ~ "Electoral",
                                         Level1 == 2 ~ "Judicial",
                                         Level1 == 3 ~ "FreeSpeech"))
#Second
data = data |> mutate(Level2 = case_when(startsWith(race, "Baseline") ~ 0,
                                           startsWith(race, "Local1") ~ 0,
                                           startsWith(race, "Local2") ~ violation2_local2,
                                           startsWith(race, "State1") ~ 0,
                                           startsWith(race, "State2") ~ violation2_state2,
                                           startsWith(race, "National1") ~ 0,
                                           startsWith(race, "National2") ~ violation2_national2))
data = data |> mutate(Level2 = case_when(Level2 == 0 ~ "Compliance",
                                         Level2 == 1 ~ "Electoral",
                                         Level2 == 2 ~ "Judicial",
                                         Level2 == 3 ~ "FreeSpeech"))


#Explorative analysis variables

#affective polarization

data = data |> mutate(affpol = abs(democrat_therm_1 - republican_therm_1))
data = data |> mutate(APstd = scale(affpol))

data = data |> mutate(voted = case_when(presvote20post == 6 ~ 0,
                                        TRUE ~ 1))

#Partisanship
data = data |> mutate(partisanship = case_when(pid7 < 4 ~ "Democrat",
                                               pid7 > 4 ~ "Republican"))

#support for displayed norms
#Level1: first level displayed

data = data |> mutate(First = case_when(startsWith(race, "Baseline") ~ compliance1_baseline,
                                         startsWith(race, "Local1") ~ violation1_local1,
                                         startsWith(race, "Local2") ~ violation1_local2,
                                         startsWith(race, "State1") ~ violation1_state1,
                                         startsWith(race, "State2") ~ violation1_state2,
                                         startsWith(race, "National1") ~ violation1_national1,
                                         startsWith(race, "National2") ~ violation1_national2))

data = data |> mutate(Second = case_when(startsWith(race, "Baseline") ~ compliance2_baseline,
                                         startsWith(race, "Local1") ~ compliance1_local1,
                                         startsWith(race, "Local2") ~ violation2_local2,
                                         startsWith(race, "State1") ~ compliance1_state1,
                                         startsWith(race, "State2") ~ violation2_state2,
                                         startsWith(race, "National1") ~ compliance1_national1,
                                         startsWith(race, "National2") ~ violation2_national2))

data = data |> mutate(L1NormSup = case_when(First == 1 ~ norm_loyalty,
                                         First == 2 ~ norm_judges,
                                         First == 3 ~ norm_censorship))

data = data |> mutate(L2NormSup = case_when(Second == 1 ~ norm_loyalty,
                                            Second == 2 ~ norm_judges,
                                            Second == 3 ~ norm_censorship))
data = data |> mutate(L1Viol = as.factor(case_when(Level1 == "Compliance" ~ 0,
                                         TRUE ~ 1)),
                      L2Viol = as.factor(case_when(Level2 == "Compliance" ~ 0,
                                         TRUE ~ 1)))

#transforming race variable, including randomized race for the baseline condition
data = data |> mutate(race = case_when(startsWith(race, "Baseline") & political_race == 1 ~ "Local",
                                       startsWith(race, "Baseline") & political_race == 2 ~ "State",
                                       startsWith(race, "Baseline") & political_race == 3 ~ "National",
                                       startsWith(race, "Local") ~ "Local",
                                       startsWith(race, "State") ~ "State",
                                       startsWith(race, "National") ~ "National"))

#variables for main analyses
data = data |> select(caseid, race, ViolNo, OutChoice, Level1, Level2, pid7,
                      affpol, vote_importance, APstd, voted, partisanship,
                      L1NormSup, L2NormSup, L1Viol, L2Viol, newsint, ideo5, urbanicity2,
                      gender, humrace, educ)

data = data |> mutate(partstrength = abs(-1*pid7+4))

